<?php

// app/Http/Middleware/CheckSiteSelected.php
namespace App\Http\Middleware;

use Closure;
use Dcat\Admin\Admin;
use Illuminate\Http\Request;

class CheckSiteSelected
{
    public function handle(Request $request, Closure $next)
    {
        // 排除的路由（支持通配符）
        $except = [
            'admin/auth*',
            'admin/set-current-site',
            'admin/websites*' // 你的站点管理路由
        ];

        if ($request->is($except)) {
            return $next($request);
        }

        if (!session('current_site_id')) {
            admin_toastr('请先选择站点', 'error');
            return redirect()->route('dcat.admin.websites.index');
        }

        Admin::context()->set('current_site_id', session('current_site_id'));


        return $next($request);
    }
}
